<?php

namespace App\Models;

use App\Common\Constant\Params;
use Illuminate\Database\Eloquent\Model;

class MerchantTradeLog extends BaseModel
{
    protected $table = 'merchant_trade_log';

    const CREATED_AT = 'add_time';
    const UPDATED_AT = 'update_time';

    protected $dateFormat = 'U';

    protected $fillable = [
        'merchant_id',
        'merchant_name',
        'trade_number',
        'direction',
        'amount',
        'type',
        'source',
        'balance',
        'order_number_list',
        'order_serial_number_list',
        'ip',
        'status',
        'error',
        'info',
        'alipay_no',
    ];

    protected $casts = [
        'amount' => 'double',
    ];

    public function setOrderNumberListAttribute($value)
    {
        $this->attributes['order_number_list'] = implode(',',(array)$value);
    }

    public function getOrderNumberListAttribute($value)
    {
        return explode(',',$value);
    }

    public function setOrderSerialNumberListAttribute($value)
    {
        $this->attributes['order_serial_number_list'] = implode(',',(array)$value);
    }

    public function getOrderSerialNumberListAttribute($value)
    {
        return explode(',',$value);
    }

    public function merchant()
    {
        return $this->belongsTo(Merchant::class,'merchant_id');
    }

    /**
     * 获取关联订单号文本
     * order_number_list_text
     */
    public function getOrderNumberListTextAttribute()
    {
        return implode(',',$this->order_number_list);
    }

    /**
     * 获取关联订单流水号文本
     * order_serial_number_list_text
     */
    public function getOrderSerialNumberListTextAttribute()
    {
        return implode(',',$this->order_serial_number_list);
    }

    /**
     * 获取交易类型文本
     * type_text
     */
    public function getTypeTextAttribute()
    {
        return Params::$MERCHANT_TRADE_TYPE[$this->type];
    }

    /**
     * 获取支付来源文本
     * source_text
     */
    public function getSourceTextAttribute()
    {
        return Params::$TRADE_SOURCE[$this->source];
    }
}
